const express = require('express'); // 搭建服务
const cors = require('cors') // 处理跨域

const app = express();
app.use(cors()) // 解决跨域问题，这里是设置所有域名都可以访问

app.get('/stream', (req, res) => {
    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');

    setInterval(() => {
        const currentTime = new Date().toLocaleTimeString();
        res.write(`data: ${currentTime}\n\n`);
    }, 1000);
});

const PORT = 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});